package fiarproject;

import java.util.Scanner;

/**
 *
 * @author Luke Richardson
 * @author Pok Ng
 */
public class UserInterface {

        /***
        * Constants for menu items
        */
        private static final int MENU_INPUT_GAME = 1;
        private static final int MENU_SHOW_RESULT = 2;
        private static final int MENU_QUIT = 9;

        private boolean quit = false;
        private Scanner input = new Scanner(System.in);
        private Game game;

    /**
     * Main menu loop
     * @throws FiarException
     */
    public void run() throws FiarException {
        inputGame();
        showResult();
//        while (!quit) {
//           printMenu();
//           parseMenuInput(input.nextLine());
//        }
    }

    /**
     * Prints main menu to StdOut
     */
    private void printMenu() {
            System.out.println(MENU_INPUT_GAME + ". Input game.");
            System.out.println(MENU_SHOW_RESULT + ". Show result of game.");
            System.out.println(MENU_QUIT + ". Quit game.");
            System.out.println("Choose an option: ");
    }

    /**
     * Prints result of game to StdOut
     */
    private void showResult() {

        System.out.println(game.getWinner()==FiarGame.BLACK?"Black":game.getWinner()==FiarGame.WHITE?"White":game.getWinner()==FiarGame.DRAW?"Draw":"None");
        System.out.println(game.getBlackWins());
        System.out.println(game.getWhiteWins());
        }

    /***
     * Initialises GameInput object and checks input
     * @throws FiarException If board width is too small or if line length is incorrect
     */
    private void inputGame() throws FiarException {
//        System.out.print("Enter width of game: ");
        int width = inputGameWidth();
        String[] boardArray = new String[width];
        for(int i = 0; i < width; i++) {
                    boardArray[i] = input.nextLine();
        }
        game = new Game(new GameInput(width, boardArray).inputGame());
    }

    /**
     * Take input of board width from user
     * @return Board width
     * @throws FiarException if board width entered is 4 or less
     */
    private int inputGameWidth() throws FiarException {
        int width = Integer.parseInt(input.nextLine());
        if (!(width > 4)) throw new FiarException("Board width error");
        return width;
    }

    private void parseMenuInput(String input) throws FiarException {
        int intInput = 0;
        try {
            intInput = Integer.parseInt(input);
        } catch (Exception e) {
            System.err.println("Choice is invalid");
        }
        try {
            switch (intInput) {
                case MENU_INPUT_GAME:
                    inputGame();
                    break;
                case MENU_SHOW_RESULT:
                    showResult();
                    break;
                case MENU_QUIT:
                    quit = true;
                    break;
                default:
            }
        } catch (FiarException e) {
            System.err.println("\n\nfiarproject.Game input error, see above.");
        }
    }
}
